import 'dart:io';

import 'package:bxultimate/data/request/base_request.dart';
import 'package:bxultimate/data/request/organization_request.dart';
import 'package:bxultimate/data/store/config_store.dart';
import 'package:bxultimate/domain/common/result.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:path_provider/path_provider.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'state.dart';

class OrganizationStatisticsTaskAnalysisController extends GetxController
    with GetSingleTickerProviderStateMixin {
  final OrganizationStatisticsTaskAnalysisState state =
      OrganizationStatisticsTaskAnalysisState();

  TabController? tabController;
  var id = Get.arguments['id'];
  var tssId = Get.arguments['tssId'];

  @override
  Future<void> onInit() async {
    // TODO: implement onInit
    super.onInit();
    // 获取数据
    state.analysis = await OrganizationRequest.getMySendStatisticsTaskInfo(id);

    state.tabs = ([
      TDTab(
        text: '已读',
        textMargin: EdgeInsets.only(right: 20, top: 2, bottom: 2),
        badge: TDBadge(
          TDBadgeType.message,
          count: "${state.analysis.readNum}",
        ),
      ),
      TDTab(
        text: '未读',
        textMargin: EdgeInsets.only(right: 20, top: 2, bottom: 2),
        badge: TDBadge(
          TDBadgeType.message,
          count: "${state.analysis.notReadNum}",
        ),
      ),
      TDTab(
        text: '已完成',
        textMargin: EdgeInsets.only(right: 20, top: 2, bottom: 2),
        badge: TDBadge(
          TDBadgeType.message,
          count: "${state.analysis.finishNum}",
        ),
      ),
      TDTab(
        text: '未完成',
        textMargin: EdgeInsets.only(right: 20, top: 2, bottom: 2),
        badge: TDBadge(
          TDBadgeType.message,
          count: "${state.analysis.notFinishNum}",
        ),
      ),
    ]);

    tabController = TabController(
      length: state.tabs.length,
      vsync: this,
    );

    state.taskInfo =
        await OrganizationRequest.getMyReceiveStatisticsTaskInfo(tssId);
    update();

    getData();
  }

  getData() async {
    state.readUserList =
        await OrganizationRequest.getMySendStatisticsTaskStatusMemberList(
            id, 'read');
    update();

    state.unreadUserList =
        await OrganizationRequest.getMySendStatisticsTaskStatusMemberList(
            id, 'notRead');
    update();
    state.unFinishUserList =
        await OrganizationRequest.getMySendStatisticsTaskStatusMemberList(
            id, 'notFinish');
    update();
    state.finishUserList =
        await OrganizationRequest.getMySendStatisticsTaskStatusMemberList(
            id, 'finish');
    update();
  }

  // 导出数据
  void export() async {
    Result result = await OrganizationRequest.exportCompleteTask(id);

    // 保存文件到本地
    SmartDialog.showNotify(msg: '导出成功', notifyType: NotifyType.success);
     Directory tempDir = await getTemporaryDirectory();
     print(tempDir.path);
    BaseRequest.downloadFile(ConfigStore.to.baseFileUrl + result.data,tempDir.path);

  }
}
